package com.cc.mydlnademo

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.cc.dlnalib.DLNAController
import com.cc.dlnalib.STATE_CUSTOM_COMPLETE
import org.cybergarage.upnp.Device

class PlayViewModel : ViewModel() {
    val tag = "PlayViewModel"
    var videoListData =
        MutableLiveData<MutableList<VideoData>>()
    var audioListData =
        MutableLiveData<MutableList<AudioData>>()
    var playTypeData = MutableLiveData<String>()
    var indexLiveData = MutableLiveData<Int>()
    var deviceLiveData = MutableLiveData<Device>()
    private val stateCallback: (state: String) -> Unit = {
        if (it == STATE_CUSTOM_COMPLETE) {
            playNext() {
                Log.i(tag, "播放成功")
            }
        }
    }

    init {
        playViewModel?.let {
            videoListData = it.videoListData
            audioListData = it.audioListData
            playTypeData = it.playTypeData
            indexLiveData = it.indexLiveData
            deviceLiveData = it.deviceLiveData
            DLNAController.removeStateCallBack(it.stateCallback)
        }
        playViewModel = this
        DLNAController.addStateCallBack(stateCallback)
    }

    companion object {
        var playViewModel: PlayViewModel? = null
        const val PLAY_TYPE_VIDEO = "video"
        const val PLAY_TYPE_AUDIO = "audio"
    }

    fun playNext(block: (isSuccess: Boolean) -> Unit) {
        val size = if (playTypeData.value == PLAY_TYPE_VIDEO) videoListData.value?.size
            ?: 0 else audioListData.value?.size ?: 0
        if (size == 0) {
            Log.i(tag, "play list size is empty")
            return
        }
        var index = indexLiveData.value ?: 0
        if (index == size - 1) {
            return
        }
        index++
        indexLiveData.value = index
        val path = getPath(index)
        if (path.isBlank()) {
            return
        }

        DLNAController.currentDevice?.let { it1 ->
            DLNAController.play(
                it1,
                path, getMetadata(index)
            ) {
                block.invoke(it)
            }
        }
    }

    fun play(
        device: Device,
        audioData: AudioData,
        bolck: ((isSuccess: Boolean) -> Unit)?
    ) {
        playTypeData.value = PLAY_TYPE_AUDIO
        audioListData.value?.let {
            indexLiveData.value = it.indexOfFirst { it._id == audioData._id }
            val index = indexLiveData.value ?: -1
            val path = getPath(index)
            val metadata = getMetadata(index)
            device.let { it1 ->
                DLNAController.play(
                    it1,
                    path,
                    metadata,
                    bolck
                )
            }
        }
    }

    fun play(
        device: Device,
        videoData: VideoData,
        bolck: ((isSuccess: Boolean) -> Unit)?
    ) {
        playTypeData.value = PLAY_TYPE_VIDEO
        videoListData.value?.let {
            indexLiveData.value = it.indexOfFirst { it._id == videoData._id }
            val index = indexLiveData.value ?: -1
            val path = getPath(index)
            val metadata = getMetadata(index)
            device.let { it1 ->
                DLNAController.play(
                    it1,
                    path,
                    metadata,
                    bolck
                )
            }
        }
    }

    fun playPre(block: (isSuccess: Boolean) -> Unit) {
        val size = if (playTypeData.value == PLAY_TYPE_VIDEO) videoListData.value?.size
            ?: 0 else audioListData.value?.size ?: 0
        if (size == 0) {
            Log.i(tag, "play list size is empty")
            return
        }
        var index = indexLiveData.value ?: 0
        if (index == 0) {
            return
        }
        index--
        indexLiveData.value = index
        val path = getPath(index)
        if (path.isBlank()) {
            return
        }

        DLNAController.currentDevice?.let { it1 ->
            DLNAController.play(
                it1,
                path, getMetadata(index)

            ) {
                block.invoke(it)
            }
        }
    }

    private fun getPath(index: Int): String {
        if (index < 0) {
            return ""
        }
        if (playTypeData.value == PLAY_TYPE_VIDEO) {
            return if (videoListData.value != null && index < videoListData.value!!.size) videoListData.value!![index].path else ""
        } else if (playTypeData.value == PLAY_TYPE_AUDIO) {
            return if (audioListData.value != null && index < audioListData.value!!.size) audioListData.value!![index].path else ""
        }
        return ""
    }

    private fun getMetadata(index: Int): String {
        if (playTypeData.value == PLAY_TYPE_VIDEO) {
            return ""
        } else if (playTypeData.value == PLAY_TYPE_AUDIO) {
            return if (audioListData.value != null && index < audioListData.value!!.size) audioListData.value!![index].createDlnaItem(
                DLNAController.ipAddress
            ) else ""
        }
        return ""
    }

    fun getCurrentPath(): String {
        return getPath(indexLiveData.value ?: -1)
    }

    fun getCurrentTitle(): String {
        val index = indexLiveData.value ?: -1
        if (playTypeData.value == PLAY_TYPE_VIDEO) {
            return if (videoListData.value != null && index < videoListData.value!!.size) videoListData.value!![index].title else ""
        } else if (playTypeData.value == PLAY_TYPE_AUDIO) {
            return if (audioListData.value != null && index < audioListData.value!!.size) audioListData.value!![index].title else ""
        }
        return ""
    }
}